package com.jv.config;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

public class UserRealm extends AuthorizingRealm {
    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("执行了授权...");
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //设置当前用户的权限，这个权限是从数据库中来的。
        info.addStringPermission("user:add");
        return info;
    }
    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //从数据库中取出来的用户名，密码
        String username = "jv";
        String password = "123";
        UsernamePasswordToken token = (UsernamePasswordToken)authenticationToken;
        //用户名认证
        if (!token.getUsername().equals(username)) {
            return null; //shiro会抛出UnknownAccountException
        }
        //密码认证交给shiro来做
        return new SimpleAuthenticationInfo(authenticationToken.getPrincipal(),password,"");
    }
}
